package com.ai.platform.core.context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;

/**
 * Created by yeyingsheng on 2018/5/1.
 * 数据范围对象持有者
 */
public class DataScopeContextHolder {

    private static final Logger logger = LoggerFactory.getLogger(DataScopeContextHolder.class);

    private static ThreadLocal<DataScopeContextHolder> contexts = new ThreadLocal<>();

    //范围名称
    private String scopeName;

    //具体值
    private List<Integer> scopeValues;

    private DataScopeContextHolder(){}

    public static void setContext(String scopeName,List<Integer> scopeValues) {
        DataScopeContextHolder rc = new DataScopeContextHolder();
        rc.scopeName=scopeName;
        rc.scopeValues=scopeValues;
        contexts.set(rc);
    }

    public static DataScopeContextHolder getContext(){
        DataScopeContextHolder ci = contexts.get();
        if (ci == null) {
            logger.debug("设置数据范围对象");
            ci = new DataScopeContextHolder();
            // 初始化参数
            contexts.set(ci);
        }
        return ci;
    }

    public static void clearContext() {
        logger.debug("清除数据范围对象");
        contexts.remove();
    }

    public String getScopeName() {
        return scopeName;
    }

    public List<Integer> getScopeValues() {
        return scopeValues;
    }

}
